%% [PredictedLabels] = CC_Classify(Samples,U,X,parameters);
% 
% Outputs:
%  PredictedLabels: column vector with labels 1..N (N = length(U)).
%
% Input:
%   Samples:  each row correspond a one sample,
%   U: struct (U{i}=1 in the portion of the space that should be labeled as
%              class i)
%   X: struct where X{k} is the kth coordinate of U{i}:R^n --> {0,1}
%   parameters: struct that may contain,
%        .verbose [def 0]
% -------------------------------------------------------------------------
% 01/5/2013, Facultad de Ingenieria - UdelaR
% Authors: G. Hernandez,  M. Fiori, A. Fernandez and M. Di Martino
% E-mail: matiasdm@fing.edu.uy
% -------------------------------------------------------------------------

function [PredictedLabels] = CC_Classify_nd(Z,U,X,parameters);

%% Load and set general parameters --------------------

if isfield(parameters, 'verbose'),
    verbose = parameters.verbose;
else % set default value
    verbose = 0;
end


% -----------------------------------------------------

%% Classify %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
NumberOfSamples = size(Z,1);
N = size(Z,2); % number of dimensions
NumberOfClasses = length(U);

% Inicialization 
PredictedLabels = zeros(NumberOfSamples,1);

for i = 1:NumberOfSamples
    v = Z(i,:); % coordinates of the sample
    
    % compute nearest neighbor
    for dim = 1:N, 
        Dn(:,dim) = X{dim} - v(dim);
    end
    
    dn = sum(Dn.^2,2); % distances to each point in the grid.
    [~,nearest] = min(dn(:));
    
    for class = 1:NumberOfClasses, 
        if U{class}(nearest) == 1;
            PredictedLabels(i) = class;
        end
    end
end
    
    
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

    
